package org.jeecg.common.util;

import com.aspose.words.Document;
import com.aspose.words.ImageSaveOptions;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

/**
 * @author jack_lee
 * @create 2020-09-22
 */
@Slf4j
public class Doc2Pdf {

    public static void doc2Img(String inPath, String outDir) {

        try {
            log.info(inPath + " -> " + outDir);
            long old = System.currentTimeMillis();
            // word文档
            Document doc = new Document(inPath);
            // 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
            ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
            int pageCount = doc.getPageCount();
            for (int i = 0; i < pageCount; i++) {
                File file = new File(outDir+"/"+i+".png");
                log.info(outDir+"/"+i+".png");
                FileOutputStream os = new FileOutputStream(file);
                options.setPageIndex(i);
                doc.save(os, options);
                os.close();
            }
            long now = System.currentTimeMillis();
            log.info("convert OK! " + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static List<HashMap<String,String>> doc2ImgFromQiniu(String url, String outDir){
        List<HashMap<String,String>>  filePahtList=null;
        try {
            log.info(url + " -> " + outDir);
            long old = System.currentTimeMillis();
            // word文档
            Document doc = new Document(url);
            log.info(doc.getOriginalFileName());
            String originalFileName=doc.getOriginalFileName();
            String name=originalFileName.substring(originalFileName.lastIndexOf("/")+1,originalFileName.lastIndexOf("."));
            log.info("ssss:"+name);
            // 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换
            ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
            int pageCount = doc.getPageCount();
            filePahtList=new ArrayList<>(pageCount+1);
            for (int i = 0; i < pageCount; i++) {
                HashMap<String,String> map=new HashMap<>();
                long timeStr = new Date().getTime();
                int max=100,min=1;
                int ran2 = (int) (Math.random()*(max-min)+min);
                Random r = new Random(i+ran2);
                Integer rd = r.nextInt(1000);
                String filePath=outDir+File.separator+name+"_"+timeStr+"_"+rd+"_"+i+".png";
                map.put("filePath",filePath);
                map.put("qiniuFileName",name+"_"+timeStr+"_"+rd+"_"+i);
                File file = new File(filePath);
                log.info(file.getName());
                FileOutputStream os = new FileOutputStream(file);
                options.setPageIndex(i);
                doc.save(os, options);
                os.close();
                filePahtList.add(map);
            }
            long now = System.currentTimeMillis();
            log.info("convert OK! " + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filePahtList;
    }
    public static HashMap<String,String> doc2Pdf(String url,String outDir){
        HashMap map=new HashMap();
        try {
            log.info(url + " -> " + outDir);
            long old = System.currentTimeMillis();
            // word文档
            Document doc = new Document(url);
            log.info(doc.getOriginalFileName());
            String originalFileName=doc.getOriginalFileName();
            String name=originalFileName.substring(originalFileName.lastIndexOf("/")+1,originalFileName.lastIndexOf("."));
            // 支持RTF HTML,OpenDocument, PDF,EPUB, XPS转换

            long timeStr = new Date().getTime();
            int max=100,min=1;
            int ran2 = (int) (Math.random()*(max-min)+min);
            Random r = new Random(ran2);
            Integer rd = r.nextInt(1000);
            String filePath=outDir+File.separator+name+"_"+timeStr+"_"+rd+".pdf";
            map.put("filePath",filePath);
            map.put("qiniuFileName",name+"_"+timeStr+"_"+rd);
            File file = new File(filePath);
            map.put("fileName",file.getName());
            log.info(file.getName());
            FileOutputStream os = new FileOutputStream(file);
            doc.save(os,SaveFormat.PDF);
            os.close();
            long now = System.currentTimeMillis();
            log.info("convert OK! " + ((now - old) / 1000.0) + "秒");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    public static void main(String[] args) {
        String tt="http://mjyjs-biz-resources.youyun8.com/%E6%B7%B1%E5%9C%B3_%E9%92%A2%E7%BB%93%E6%9E%84%E5%88%86%E5%8C%85%E5%B7%A5%E7%A8%8B%E6%96%BD%E5%B7%A5%E7%BB%84%E7%BB%87%E8%AE%BE%E8%AE%A1323%E9%A1%B5.doc";
        doc2Pdf(tt,"C:/Users/jack_lee/Desktop/12345");
        doc2Pdf(tt,"C:/Users/jack_lee/Desktop/12345");
    }

}
